package com.xhui.vimarket.zkplus.databind;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zkplus.databind.DataBinder;

abstract public class GenericDataBinderComposer extends GenericForwardComposer {
    private static final long serialVersionUID = -8962566563467903754L;
    protected DataBinder      binder;
    
    @Override
    public void doAfterCompose(final Component comp) throws Exception {
        super.doAfterCompose(comp);
        this.binder = new AnnotateDataBinder(comp);
        comp.setAttribute("binder", this.binder);
    }
    
    protected void loadAll() {
        this.binder.loadAll();
    }
    
    protected void loadAttribute(final Component comp, final String attr) {
        this.binder.loadAttribute(comp, attr);
    }
    
    protected void loadComponent(final Component comp) {
        this.binder.loadComponent(comp);
    }
    
    /**  
     * 绑定bean到ui组件上  
     * <p>  
     * 范例  
     * <hr />  
     *   
     * <pre>  
     * <code>  
     * &lt;window id=&quot;userWin&quot; apply=&quot;GenericDataBinderComposerChild&quot;&gt;  
     *    &lt;textbox id=&quot;txtFirstName&quot; /&gt;  
     *    &lt;textbox id=&quot;txtlastName&quot; /&gt;  
     *    &lt;label id=&quot;lblFullName&quot; /&gt;  
     * &lt;/window&gt;  
     * GenericDataBinderComposerChild类  
     *   
     * public GenericDataBinderComposerChild extends GenericDataBinderComposer{  
     *      private Textbox txtFirstName;  
     *      private Textbox txtLastName;  
     *      private Label lblFullName;  
     *   
     *      public void doAfterCompose(Component comp) throws Exception {  
     *          super.doAfterCompose(comp);  
     *          binder.addBinding(txtFirstName, "value", "userWin$composer.user.firstName");  
     *          binder.addBinding(txtLastName, "value", "userWin$composer.user.lastName");  
     *   
     *          //"txtLastName.onBlur"中的txtLastName是页面上&lt;textbox id=&quot;txtlastName&quot; /&gt;  
     *          //userWin$composer中的userWin是页面上window的id，$.composer是固定格式  
     *          addBinding(lblFullName, "value", "userWin$composer.user.fullName","txtLastName.onBlur" , null,null,null);  
     *      }  
     * }  
     * </pre>  
     *   
     * @param comp  
     *            ui组件  
     * @param attr  
     *            ui组件的属性，例如&lt;textbox value="@{user.name}"/&gt;中的value  
     * @param expr  
     *            表达式，例如，例如&lt;textbox value="@{user.name}"/&gt;中的user.name  
     * @param loadWhenEvents  
     *            加载bean到ui的时机，例如comp1.onClck、comp2、onBlur、comp3.onSelect,  
     *            等同于&lt;textbox id=&quot;txtFullName&quot;  
     *            value=&quot;@{user.fullName, load-when=   
     *            'txtLastName.onBlur'}&quot;/&gt;中的txtLastName.onBlur  
     * @param saveWhenEvent  
     *            保存ui value到bean的时机，例如comp3.onBlur, 等同于&lt;textbox  
     *            id=&quot;txtFullName&quot; value=&quot;@{user.fullName,  
     *            save-when=   
     *            'txtFullName.onBlur',}&quot;/&gt;中的txtFullName.onBlur  
     * @param access  
     *            访问安全性，允许保存ui value 到bean，还是是load bean到ui,还是两者都是，或者什么都不做，  
     *            可选值为none,save,load,both(save/load),  
     * @param converter  
     *            转换器，ui到bean之间的互相转换，类似hibernate jdbc type到db type或者struts中的转换器  
     *            ，样例见{@link SexRadiogroupConverter}  
     */
    protected void addBinding(final Component comp, final String attr, final String expr,
        final String loadWhenEvent, final String saveWhenEvent, final String access,
        final String converter) {
        if (loadWhenEvent != null && "".equals(loadWhenEvent.trim())) {
            this.binder.addBinding(comp, attr, expr, (List) null, saveWhenEvent, access, converter);
        } else {
            this.binder.addBinding(comp, attr, expr, new String[] {loadWhenEvent }, saveWhenEvent,
                access, converter);
        }
        
    }
    
    /**  
     * 绑定bean到ui组件上  
     * <p>  
     * 范例  
     * <hr />  
     *   
     * <pre>  
     * 页面  
     * &lt;window id=&quot;userWin&quot; apply=&quot;GenericDataBinderComposerChild&quot;&gt;  
     *    &lt;textbox id=&quot;txtFirstName&quot; /&gt;  
     *    &lt;textbox id=&quot;txtlastName&quot; /&gt;  
     *    &lt;label id=&quot;lblFullName&quot; /&gt;  
     * &lt;/window&gt;  
     * GenericDataBinderComposerChild类  
     *   
     * public GenericDataBinderComposerChild extends GenericDataBinderComposer{  
     *      private Textbox txtFirstName;  
     *      private Textbox txtLastName;  
     *      private Label lblFullName;  
     *   
     *      public void doAfterCompose(Component comp) throws Exception {  
     *          super.doAfterCompose(comp);  
     *          binder.addBinding(txtFirstName, "value", "userWin$composer.user.firstName");  
     *          binder.addBinding(txtLastName, "value", "userWin$composer.user.lastName");  
     *   
     *          //"txtLastName.onBlur"中的txtLastName是页面上&lt;textbox id=&quot;txtlastName&quot; /&gt;  
     *          //userWin$composer中的userWin是页面上window的id，$.composer是固定格式  
     *          addBinding(lblFullName, "value", "userWin$composer.user.fullName","txtLastName.onBlur" , null,null,null);  
     *      }  
     * }  
     * </pre>  
     *   
     * @param comp  
     *            ui组件  
     * @param attr  
     *            ui组件的属性，例如&lt;textbox value="@{user.name}"/&gt;中的value  
     * @param expr  
     *            表达式，例如，例如&lt;textbox value="@{user.name}"/&gt;中的user.name  
     * @param access  
     *            访问安全性，允许保存ui value 到bean，还是是load bean到ui,还是两者都是，或者什么都不做，  
     *            可选值为none,save,load,both(save/load),  
     */
    protected void addBinding(final Component comp, final String attr, final String expr,
        final String access) {
        this.binder.addBinding(comp, attr, expr, (List) null, (List) null, access, null);
    }
    
    /**  
     * 绑定bean到ui组件上  
     * <p>  
     * 范例  
     * <hr />  
     *   
     * <pre>  
     * &lt;window id=&quot;userWin&quot; apply=&quot;GenericDataBinderComposerChild&quot;&gt;  
     *    &lt;textbox id=&quot;txtFirstName&quot; /&gt;  
     *    &lt;textbox id=&quot;txtlastName&quot; /&gt;  
     *    &lt;label id=&quot;lblFullName&quot; /&gt;  
     * &lt;/window&gt;  
     * GenericDataBinderComposerChild类  
     *   
     * public GenericDataBinderComposerChild extends GenericDataBinderComposer{  
     *      private Textbox txtFirstName;  
     *      private Textbox txtLastName;  
     *      private Label lblFullName;  
     *   
     *      public void doAfterCompose(Component comp) throws Exception {  
     *          super.doAfterCompose(comp);  
     *          binder.addBinding(txtFirstName, "value", "userWin$composer.user.firstName");  
     *          binder.addBinding(txtLastName, "value", "userWin$composer.user.lastName");  
     *   
     *          //"txtLastName.onBlur"中的txtLastName是页面上&lt;textbox id=&quot;txtlastName&quot; /&gt;  
     *          //userWin$composer中的userWin是页面上window的id，$.composer是固定格式  
     *          addBinding(lblFullName, "value", "userWin$composer.user.fullName","txtLastName.onBlur" , null,null,null);  
     *      }  
     * }  
     *   
     * </pre>  
     *   
     * @param comp  
     *            ui组件  
     * @param attr  
     *            ui组件的属性，例如&lt;textbox value="@{user.name}"/&gt;中的value  
     * @param expr  
     *            表达式，例如，例如&lt;textbox value="@{user.name}"/&gt;中的user.name  
     * @param loadWhenEvents  
     *            加载bean到ui的时机，例如comp1.onClck、comp2、onBlur、comp3.onSelect,  
     *            等同于&lt;textbox id=&quot;txtFullName&quot;  
     *            value=&quot;@{user.fullName, load-when=   
     *            'txtLastName.onBlur'}&quot;/&gt;中的txtLastName.onBlur  
     * @param access  
     *            访问安全性，允许保存ui value 到bean，还是是load bean到ui,还是两者都是，或者什么都不做，  
     *            可选值为none,save,load,both(save/load),  
     * @param converter  
     *            转换器，ui到bean之间的互相转换，类似hibernate jdbc type到db type或者struts中的转换器  
     *            ，样例见{@link SexRadiogroupConverter}  
     */
    protected void addBinding(final Component comp, final String attr, final String expr,
        final String access, final String converter) {
        this.binder.addBinding(comp, attr, expr, (List) null, (List) null, access, converter);
    }
    
}
